package com.bawey.vmusic;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import com.perm.kate.api.Api;
import com.perm.kate.api.Audio;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// no test - just launch login
		Intent intent = new Intent(this, AuthActivity.class);
		startActivityForResult(intent, AuthActivity.REQUEST_LOGIN);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(AuthActivity.REQUEST_LOGIN, Activity.RESULT_OK,
				data);

		Globals.setTOKEN(data.getExtras().getString(Globals.KEY_TOKEN));
		Globals.setUSER_ID(data.getExtras().getString(Globals.KEY_USER_ID));
		Globals.setApi(new Api(Globals.getTOKEN(), Globals.APP_ID));

		// test sth
		try {
			List<Audio> audios = Globals.getApi().searchAudio("offspring", "1",
					"0", new Long(Globals.MAX_AUDIO_SEARCH), new Long(0));
			StringBuffer sb = new StringBuffer();
			for (Audio a : audios) {
				sb.append(a.artist);
				sb.append(" ");
				sb.append(a.title);
				sb.append("\n");
			}
			((TextView) findViewById(R.id.mainStatus)).setText(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
